<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 项目实训课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>表单的渲染 | 项目实训课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../data/form/form_valid.html" />
    
    
    <link rel="prev" href="../../data/form/form_define.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.2"
        data-chapter-title="表单的渲染"
        data-filepath="data/form/form.md"
        data-basepath="../.."
        data-revision="Fri Apr 26 2019 21:51:37 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        博学谷在线
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="data/form/index.html">
            
                
                    <a href="../../data/form/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django表单系统
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="data/form/form_define.html">
            
                
                    <a href="../../data/form/form_define.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        表单的定义
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.2" data-path="data/form/form.html">
            
                
                    <a href="../../data/form/form.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        表单的渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="data/form/form_valid.html">
            
                
                    <a href="../../data/form/form_valid.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        表单数据的验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="data/form/Captcha.html">
            
                
                    <a href="../../data/form/Captcha.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        验证码模块
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="data/path.html">
            
                
                    <a href="../../data/path.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        环境搭建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="data/data.html">
            
                
                    <a href="../../data/data.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据录入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="data/index.html">
            
                
                    <a href="../../data/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        首页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="data/register.html">
            
                
                    <a href="../../data/register.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        用户注册
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="data/login.html">
            
                
                    <a href="../../data/login.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        用户登录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="data/forget_pwd.html">
            
                
                    <a href="../../data/forget_pwd.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        忘记密码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="data/change_pwd.html">
            
                
                    <a href="../../data/change_pwd.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        修改密码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="data/user/user_info.html">
            
                
                    <a href="../../data/user/user_info.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        用户中心
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="data/user/user_class.html">
            
                
                    <a href="../../data/user/user_class.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        我的课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="data/user/user_collect.html">
            
                
                    <a href="../../data/user/user_collect.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        我的收藏
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="data/user/user_msg.html">
            
                
                    <a href="../../data/user/user_msg.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        我的消息
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >项目实训课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x8868;&#x5355;&#x6A21;&#x677F;&#x6E32;&#x67D3;&#x7684;&#x4F7F;&#x7528;">&#x8868;&#x5355;&#x6A21;&#x677F;&#x6E32;&#x67D3;&#x7684;&#x4F7F;&#x7528;</h1>
<p>&#x6211;&#x4EEC;&#x5728;&#x9700;&#x8981;&#x6CE8;&#x518C;&#x89C6;&#x56FE;&#x8C03;&#x7528;&#x6A21;&#x7248;&#x6765;&#x6E32;&#x67D3;&#x751F;&#x6210;&#x6CE8;&#x518C;&#x8868;&#x5355;&#x9875;&#x9762;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render
<span class="hljs-keyword">from</span> django.views <span class="hljs-keyword">import</span> View
<span class="hljs-keyword">from</span> users.forms <span class="hljs-keyword">import</span> RegisterForm
<span class="hljs-comment"># Create your views here.</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserRegister</span><span class="hljs-params">(View)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self,request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
            &#x83B7;&#x53D6;&#x6CE8;&#x518C;&#x9875;&#x9762;
        :param request:
        :return:
        &quot;&quot;&quot;</span>
        <span class="hljs-comment"># &#x751F;&#x6210;&#x8868;&#x5355;&#x5BF9;&#x8C61;</span>
        register_form=RegisterForm()
        <span class="hljs-comment"># &#x8C03;&#x7528;&#x6A21;&#x7248;&#x6E32;&#x67D3;&#x751F;&#x6210;&#x8868;&#x5355;</span>
        <span class="hljs-keyword">return</span> render(request,<span class="hljs-string">&apos;register.html&apos;</span>,{<span class="hljs-string">&apos;register_form&apos;</span>:register_form})
</code></pre>
<p>&#x6A21;&#x7248;&#x7684;&#x6E32;&#x67D3;&#xFF0C;&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x521B;&#x5EFA;register.html&#x6765;&#x6E32;&#x67D3;&#x6570;&#x636E;</p>
<pre><code class="lang-html"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> <span class="hljs-attribute">lang</span>=<span class="hljs-value">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;/register/&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;post&quot;</span>&gt;</span>
  {#&#x83B7;&#x53D6;&#x89C6;&#x56FE;&#x4E2D;&#x4F20;&#x9012;&#x8FC7;&#x6765;&#x7684;register_form&#x5BF9;&#x8C61;&#x76F4;&#x63A5;&#x8FDB;&#x884C;&#x52A0;&#x8F7D;&#x6E32;&#x67D3;#}
    {{ register_form }}
    {% csrf_token %}
    <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">&quot;&#x6CE8;&#x518C;&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</code></pre>
<p>&#x914D;&#x7F6E;&#x5B50;&#x8DEF;&#x7531;&#x548C;&#x5168;&#x5C40;&#x8DEF;&#x7531;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5B50;&#x8DEF;&#x7531;&#x914D;&#x7F6E;</span>
<span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> . <span class="hljs-keyword">import</span> views
urlpatterns = [
    url(<span class="hljs-string">r&apos;^register/&apos;</span>, views.UserRegister.as_view()),
]


<span class="hljs-comment">#  &#x5168;&#x5C40;&#x8DEF;&#x7531;&#x914D;&#x7F6E;</span>
<span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url,include
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin

urlpatterns = [
    url(<span class="hljs-string">r&apos;^admin/&apos;</span>, admin.site.urls),
    url(<span class="hljs-string">r&apos;^&apos;</span>, include(<span class="hljs-string">&apos;users.urls&apos;</span>)),
]
</code></pre>
<p><strong>&#x8BF7;&#x6C42;&#x540E;&#x6548;&#x679C;&#x5982;&#x4E0B;</strong>&#xFF1A;</p>
<p><img src="../../src/show_form.png" alt="show_form"></p>
<p>&#x6211;&#x4EEC;&#x5728;&#x5B9E;&#x9645;&#x4E2D;&#x9700;&#x8981;&#x5BF9;&#x8868;&#x5355;&#x6837;&#x5F0F;&#x8FDB;&#x884C;&#x63A7;&#x5236;&#xFF0C;&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x5BF9;&#x6A21;&#x677F;&#x7684;&#x5C55;&#x793A;&#x6837;&#x5F0F;&#x8FDB;&#x884C;&#x63A7;&#x5236;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x6765;&#x751F;&#x6210;&#x8868;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6309;&#x7167;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;&#x751F;&#x6210;&#x8868;&#x5355;:</p>
<pre><code class="lang-html"><span class="hljs-doctype">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">html</span> <span class="hljs-attribute">lang</span>=<span class="hljs-value">&quot;en&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">meta</span> <span class="hljs-attribute">charset</span>=<span class="hljs-value">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">title</span>&gt;</span>Title<span class="hljs-tag">&lt;/<span class="hljs-title">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">form</span> <span class="hljs-attribute">action</span>=<span class="hljs-value">&quot;/register/&quot;</span> <span class="hljs-attribute">method</span>=<span class="hljs-value">&quot;post&quot;</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
        {#   register_form.username.label &#x83B7;&#x53D6;&#x8868;&#x5355;&#x4E2D;username&#x5B57;&#x6BB5;&#x7684;label&#x9009;&#x9879;&#x6240;&#x6307;&#x5B9A;&#x7684;&#x540D;&#x79F0;  #}
        <span class="hljs-tag">&lt;<span class="hljs-title">lable</span>&gt;</span> {{ register_form.username.label }}:<span class="hljs-tag">&lt;/<span class="hljs-title">lable</span>&gt;</span>
        {#    register_form.username.html_name &#x83B7;&#x53D6;&#x8868;&#x5355;&#x4E2D;&#x7684;username&#x5B57;&#x6BB5;&#x540D;  #}
        <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;{{ register_form.username.html_name }}&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">lable</span>&gt;</span> {{ register_form.password.label }}:<span class="hljs-tag">&lt;/<span class="hljs-title">lable</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;{{ register_form.password.html_name }}&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">lable</span>&gt;</span> {{ register_form.mobile.label }}:<span class="hljs-tag">&lt;/<span class="hljs-title">lable</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;text&quot;</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">&quot;{{ register_form.mobile.html_name }}&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
    {% csrf_token %}

    <span class="hljs-tag">&lt;<span class="hljs-title">input</span> <span class="hljs-attribute">type</span>=<span class="hljs-value">&quot;submit&quot;</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">&quot;&#x6CE8;&#x518C;&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-title">form</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">html</span>&gt;</span>
</code></pre>
<p>&#x901A;&#x8FC7;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5199;css&#x4EE3;&#x7801;&#x6765;&#x63A7;&#x5236;&#x6837;&#x5F0F;&#xFF0C;&#x6548;&#x679C;&#x5982;&#x4E0B;</p>
<p><img src="../../src/show_form1.png" alt="show_form1"></p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../data/form/form_define.html" class="navigation navigation-prev " aria-label="Previous page: 表单的定义"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../data/form/form_valid.html" class="navigation navigation-next " aria-label="Next page: 表单数据的验证"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © ITCast","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 项目实训课程讲义 -->
</html>
